#!/bin/bash
##############################################################
source ./pkgutils.sh || exit 1
PKG_NAME=$(pkgname); if (( $? != 0)); then exit 2; fi
PKG_VERSION=$(pkgversion); if (( $? != 0)); then exit 3; fi
if [[ $(whoami) != "root" ]]; then
	echo "Please log in as user root...";
	exit 4;
fi
mkdir -pv ./tmp && cd ./tmp || exit 5;
echo "setup of $PKG_NAME $PKG_VERSION";
##############################################################
# do setup tasks (location: /usr/pkg/$PKG_NAME/$PKG_VERSION/tmp)
# BLK1
[[ $(cat /etc/group | grep '^smmsp:') == "" ]] || groupadd -g 26 smmsp && \
[[ $(cat /etc/passwd | grep '^smmsp:') == "" ]] || useradd -c "Sendmail Daemon" -g smmsp -d /dev/null \
        -s /bin/false -u 26 smmsp && \
chmod -v 1777 /var/mail && \
[ -d /var/spool/mqueue ] || echo "/var/spool/mqueue" >> /usr/pkg/installed.db/${PKG_NAME}_${PKG_VERSION}_pkg.files && \
install -v -m700 -d /var/spool/mqueue && \
[ -e /etc/mail/local-host-names ] || {
    echo $(hostname) > /etc/mail/local-host-names && \
    echo "/etc/mail/local-host-names" >> /usr/pkg/installed.db/${PKG_NAME}_${PKG_VERSION}_pkg.files;
} && \
[ -e /etc/mail/aliases ] || {
    echo "postmaster: root
MAILER-DAEMON: root
" > /etc/mail/aliases && \
    echo "/etc/mail/aliases" >> /usr/pkg/installed.db/${PKG_NAME}_${PKG_VERSION}_pkg.files && \
    newaliases;
} && \
[ -e /etc/mail/sendmail.cf ] || {
    cd /etc/mail && \
    m4 m4/cf.m4 sendmail.mc > sendmail.cf && \
    echo "/etc/mail/sendmail.cf" >> /usr/pkg/installed.db/${PKG_NAME}_${PKG_VERSION}_pkg.files;
}

# hint: write all file modifications/copy/mv/ln into: /usr/pkg/installed.db/${PKG_NAME}_${PKG_VERSION}_pkg.files
##############################################################
ERR=$?;
if (($ERR != 0)); then
    errcho "Aborted package setup with code $ERR";
    exit 6;
fi
